﻿package com.sms.monitor.utils;

import java.util.Arrays;

import com.sms.monitor.constant.Constant;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

/**
 * 
 * 保存配置信息到本地。
 * 
 */
public class SharedPreferencesUtil {
    /**
     * 
     * @param context
     * @param key
     * @return
     */
    public static String getValue(Context context, String key) {
        SharedPreferences spf = context.getSharedPreferences(Constant.KEY_SMS_SPF, Context.MODE_PRIVATE);
        return spf.getString(key, "");
    }

    public static void putValue(Context context, String key, String value) {
        SharedPreferences spf = context.getSharedPreferences(Constant.KEY_SMS_SPF, Context.MODE_PRIVATE);
        Editor editor = spf.edit();
        editor.putString(key, value);
        editor.commit();
    }

    public static void putBooleanValue(Context context, String key, boolean value) {
        SharedPreferences spf = context.getSharedPreferences(Constant.KEY_SMS_SPF, Context.MODE_PRIVATE);
        Editor editor = spf.edit();
        editor.putBoolean(key, value);
        editor.commit();
    }

    public static boolean getBooleanValue(Context context, String key) {
        SharedPreferences spf = context.getSharedPreferences(Constant.KEY_SMS_SPF, Context.MODE_PRIVATE);
        return spf.getBoolean(key, true);
    }

    /** 保存提示过的id */
    public static void putSMSIds(Context context, int id) {
        SharedPreferences spf = context.getSharedPreferences(Constant.KEY_SMS_TO_IDS, Context.MODE_PRIVATE);
        StringBuilder sb = new StringBuilder(spf.getString(Constant.KEY_SMS_TO_IDS, ""));
        if (sb.length() > 1024 * 2) {
            sb.delete(0, sb.length());
        }
        sb.append(id).append(",");

        ULog.d(sb.toString());

        Editor editor = spf.edit();
        editor.putString(Constant.KEY_SMS_TO_IDS, sb.toString());
        editor.commit();
    }

    /** 判断该id是否提示过 */
    public static boolean chkSMSIds(Context context, int id) {
        String sId = Integer.toString(id);
        SharedPreferences spf = context.getSharedPreferences(Constant.KEY_SMS_TO_IDS, Context.MODE_PRIVATE);
        String[] sb = spf.getString(Constant.KEY_SMS_TO_IDS, "").split(",");
        ULog.d(Arrays.toString(sb));

        for (String s : sb) {
            if (sId.equals(s)) {
                return true;
            }
        }
        return false;
    }
}
